package neo4j.learn.Repository.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;


@Repository
public class MysqlDaoImpl<T> extends HibernateDaoSupport implements MysqlDao<T> {
	
	
	protected Session session;

	@Resource
	public void setSessionFactory0(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}

	public boolean save(T t) {
		// 获取Session对象
		session = SessionUtils.openSession(getSessionFactory());
		// 判断Session是否为空
		if (!SessionUtils.isSessionNull(session)) {
			session.save(t);
			// 关闭Session连接
			SessionUtils.CloseSession(session);
			return true;
		}
		return false;
	}

	public boolean delte(T t) {
		session = SessionUtils.openSession(getSessionFactory());
		if(!SessionUtils.isSessionNull(session)){
			session.delete(t);
			SessionUtils.CloseSession(session);
			return true;
		}
		return false;
	}

	public T findOne(String str) {
		session = SessionUtils.openSession(getSessionFactory());
	if(!SessionUtils.isSessionNull(session)){
		Query query = session.createQuery(str);
		List<T>list = query.list();
		SessionUtils.CloseSession(session);
		if(list.size()>0){
			return list.get(0);
			}
		}
		return null;
	}

	public List<T> findList(String str) {
		session = SessionUtils.openSession(getSessionFactory());
	if(!SessionUtils.isSessionNull(session)){
		Query query = session.createQuery(str);
		List<T>list = query.list();
		SessionUtils.CloseSession(session);
		if(list.size()>0){
			return list;
			}
		}
		return null;
	}
	

}
